package cn.tedu.vrd07.mapper;

import cn.tedu.vrd07.entity.Product;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ProductMapper {
    @Insert("insert into product values(null,#{title},#{author}," +
            "#{intro},#{url},0,0,#{created},#{categoryId})")
    void insert(Product product);

    @Select("select id,title,author,url,created from product")
    List<Product> select();

    @Delete("delete from product where id=#{id}")
    void delete(int id);

    /*如果查询出的字段名和装数据对象的属性不一致的情况下需要通过@Result注解
    * 单独指定一下,否则查询不到数据*/
    @Select("select id,title,url,view_count,like_count from product")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> selectForIndex();

    @Select("select id,title,url,view_count,like_count from product where category_id=#{cid}")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> selectByCid(int cid);

    @Select("select id,title,url,view_count,like_count from product where title like concat('%',#{kw},'%')")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    List<Product> selectByKw(String kw);

    @Select("select id,title,intro,url from product " +
            "order by view_count desc limit 0,4")
    List<Product> selectView();

    @Select("select id,title,intro,url from product " +
            "order by like_count desc limit 0,4")
    List<Product> selectLike();

    @Select("select id,title,author,intro,url,view_count,like_count,created from product where id=#{id}")
    @Result(column = "view_count",property = "viewCount")
    @Result(column = "like_count",property = "likeCount")
    Product detail(int id);

    @Update("update product set like_count=like_count+1  where id=#{id}")
    void likeById(int id);

    @Update("update product set view_count=view_count+1  where id=#{id}")
    void viewById(int id);

    @Select("select url from product where id=#{id}")
    String selectById(int id);

    @Select("select url from product where category_id=#{id}")
    List<String> selectUrlsByCid(int id);

    //返回值int 代表生效行数
    @Delete("delete from product where category_id=#{id}")
    int deleteByCid(int id);
}
